// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CommonUserWidget.h"

#include "MaterialProgressBar.generated.h"

class UImage;
class UMaterialInstanceDynamic;
class UMaterialInterface;
class UWidgetAnimation;

UCLASS(Abstract, meta = (DisableNativeTick))
class UMaterialProgressBar : public UCommonUserWidget
{
	GENERATED_BODY()

protected:

	/**
	 * 同步属性，当控件属性在编辑器或运行时发生变化时调用
	 */
	virtual void SynchronizeProperties() override;

#if WITH_EDITOR
	/**
	 * 当控件重建时调用，用于编辑器中的初始化
	 */
	virtual void OnWidgetRebuilt() override;
#endif

	/**
	 * 动画完成时的回调函数
	 * @param Animation 完成的动画对象
	 */
	virtual void OnAnimationFinished_Implementation(const UWidgetAnimation* Animation) override;

public:

	/**
	 * 设置进度条进度
	 * @param Progress 进度值（0-1）
	 */
	UFUNCTION(BlueprintCallable)
	void SetProgress(float Progress);

	/**
	 * 设置进度条起始进度
	 * @param StartProgress 起始进度值（0-1）
	 */
	UFUNCTION(BlueprintCallable)
	void SetStartProgress(float StartProgress);

	/**
	 * 设置颜色A（通常是进度条起始颜色）
	 * @param ColorA 颜色值
	 */
	UFUNCTION(BlueprintCallable)
	void SetColorA(FLinearColor ColorA);

	/**
	 * 设置颜色B（通常是进度条结束颜色）
	 * @param ColorB 颜色值
	 */
	UFUNCTION(BlueprintCallable)
	void SetColorB(FLinearColor ColorB);

	/**
	 * 设置背景颜色
	 * @param ColorBackground 背景颜色值
	 */
	UFUNCTION(BlueprintCallable)
	void SetColorBackground(FLinearColor ColorBackground);

	/**
	 * 从指定起始点动画填充进度条
	 * @param Start 起始进度
	 * @param End 结束进度
	 * @param AnimSpeed 动画速度
	 */
	UFUNCTION(BlueprintCallable)
	void AnimateProgressFromStart(float Start, float End, float AnimSpeed = 1.0f);

	/**
	 * 从当前进度动画填充到指定进度
	 * @param End 结束进度
	 * @param AnimSpeed 动画速度
	 */
	UFUNCTION(BlueprintCallable)
	void AnimateProgressFromCurrent(float End, float AnimSpeed = 1.0f);

	/**
	 * 填充动画完成时的委托
	 */
	DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnFillAnimationFinished);

	UPROPERTY(BlueprintAssignable)
	FOnFillAnimationFinished OnFillAnimationFinished; // 填充动画完成事件

private:

	/**
	 * 内部设置进度
	 * @param Progress 进度值
	 */
	void SetProgress_Internal(float Progress);
	
	/**
	 * 内部设置起始进度
	 * @param StartProgress 起始进度值
	 */
	void SetStartProgress_Internal(float StartProgress);
	
	/**
	 * 内部设置颜色A
	 * @param ColorA 颜色值
	 */
	void SetColorA_Internal(FLinearColor ColorA);
	
	/**
	 * 内部设置颜色B
	 * @param ColorB 颜色值
	 */
	void SetColorB_Internal(FLinearColor ColorB);
	
	/**
	 * 内部设置背景颜色
	 * @param ColorBackground 背景颜色值
	 */
	void SetColorBackground_Internal(FLinearColor ColorBackground);

	/**
	 * 获取进度条动态材质实例
	 * @return 动态材质实例指针
	 */
	UMaterialInstanceDynamic* GetBarDynamicMaterial() const;

	UPROPERTY(EditAnywhere, meta = (InlineEditConditionToggle = "CachedColorA"))
	bool bOverrideDefaultColorA = false; // 是否覆盖默认颜色A

	UPROPERTY(EditAnywhere, meta = (DisplayName = "Color A", EditCondition = "bOverrideDefaultColorA"))
	FLinearColor CachedColorA; // 缓存的颜色A值

	UPROPERTY(EditAnywhere, meta = (InlineEditConditionToggle = "CachedColorB"))
	bool bOverrideDefaultColorB = false; // 是否覆盖默认颜色B

	UPROPERTY(EditAnywhere, meta = (DisplayName = "Color B", EditCondition = "bOverrideDefaultColorB"))
	FLinearColor CachedColorB; // 缓存的颜色B值

	UPROPERTY(EditAnywhere, meta = (InlineEditConditionToggle = "CachedColorBackground"))
	bool bOverrideDefaultColorBackground; // 是否覆盖默认背景颜色

	UPROPERTY(EditAnywhere, meta = (DisplayName = "Color Background", EditCondition = "bOverrideDefaultColorBackground"))
	FLinearColor CachedColorBackground; // 缓存的背景颜色值

	UPROPERTY(EditAnywhere, meta = (InlineEditConditionToggle = "Segments"))
	bool bOverrideDefaultSegments = false; // 是否覆盖默认分段数

	UPROPERTY(EditAnywhere, meta = (EditCondition = "bOverrideDefaultSegments"))
	int32 Segments; // 分段数量

	UPROPERTY(EditAnywhere, meta = (InlineEditConditionToggle = "SegmentEdge"))
	bool bOverrideDefaultSegmentEdge = false; // 是否覆盖默认分段边缘

	UPROPERTY(EditAnywhere, meta = (EditCondition = "bOverrideDefaultSegmentEdge"))
	float SegmentEdge; // 分段边缘值

	UPROPERTY(EditAnywhere, meta = (InlineEditConditionToggle = "FillEdgeSoftness"))
	bool bOverrideDefaultFillEdgeSoftness; // 是否覆盖默认填充边缘柔化

	UPROPERTY(EditAnywhere, meta = (EditCondition = "bOverrideDefaultFillEdgeSoftness"))
	float FillEdgeSoftness; // 填充边缘柔化值

	UPROPERTY(EditAnywhere, meta = (InlineEditConditionToggle = "GlowEdge"))
	bool bOverrideDefaultGlowEdge = false; // 是否覆盖默认发光边缘

	UPROPERTY(EditAnywhere, meta = (EditCondition = "bOverrideDefaultGlowEdge"))
	float GlowEdge; // 发光边缘值

	UPROPERTY(EditAnywhere, meta = (InlineEditConditionToggle = "GlowSoftness"))
	bool bOverrideDefaultGlowSoftness = false; // 是否覆盖默认发光柔化

	UPROPERTY(EditAnywhere, meta = (EditCondition = "bOverrideDefaultGlowSoftness"))
	float GlowSoftness; // 发光柔化值

	UPROPERTY(EditAnywhere, meta = (InlineEditConditionToggle = "OutlineScale"))
	bool bOverrideDefaultOutlineScale = false; // 是否覆盖默认轮廓缩放

	UPROPERTY(EditAnywhere, meta = (EditCondition = "bOverrideDefaultOutlineScale"))
	float OutlineScale; // 轮廓缩放值

	UPROPERTY(EditAnywhere)
	bool bUseStroke = true; // 是否使用描边材质

	UPROPERTY(EditDefaultsOnly)
	TObjectPtr<UMaterialInterface> StrokeMaterial; // 描边材质引用

	UPROPERTY(EditDefaultsOnly)
	TObjectPtr<UMaterialInterface> NoStrokeMaterial; // 无描边材质引用

#if WITH_EDITORONLY_DATA
	UPROPERTY(EditAnywhere, meta = (DisplayName = "Design Time Progress"))
	float DesignTime_Progress = 1.0f; // 设计时进度值
#endif

	UPROPERTY(BlueprintReadOnly, meta = (BindWidget, AllowPrivateAccess))
	TObjectPtr<UImage> Image_Bar; // 进度条图像控件

	UPROPERTY(BlueprintReadOnly, Transient, meta = (BindWidgetAnim, AllowPrivateAccess))
	TObjectPtr<UWidgetAnimation> BoundAnim_FillBar; // 绑定的填充动画

	UPROPERTY(Transient)
	mutable TObjectPtr<UMaterialInstanceDynamic> CachedMID; // 缓存的动态材质实例

	float CachedProgress = -1.0f; // 缓存的进度值
	float CachedStartProgress = -1.0f; // 缓存的起始进度值
};